import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class P07_Panel extends JPanel  implements MouseListener, MouseMotionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[12];
	
	public int man=0;
	
	public int position =0;
	
	private boolean moving=false;
	
	public P07_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/07.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		imageList[0]=new ImageIcon(getClass().getResource("images/7/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/7/head.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/7/table.png")).getImage();
		
		imageList[3]=new ImageIcon(getClass().getResource("images/7/00.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/7/01.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/7/02.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/7/03.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/7/04.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/7/05.png")).getImage();
		imageList[9]=new ImageIcon(getClass().getResource("images/7/06.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/7/07.png")).getImage();
		imageList[11]=new ImageIcon(getClass().getResource("images/7/08.png")).getImage();
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
		
		//head
		g.drawImage(imageList[1], 422, 400, null);
		
		//table
		g.drawImage(imageList[2], 395, 578, null);
		
		//cells
		//3-11
		g.drawImage(imageList[3+position], 501, 575, null);
		
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
		
		for(int i=0;i<9;i++){
			if(501+107*i<e.getX() && e.getX()<501+107*(i+1) 
					&& 575<e.getY() && e.getY()<575+64){
				position=i;
			}
		}
				
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	if(menu_presseed[0] &&1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				
				listener.onChangeValue(21, position);
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
    	for(int i=0;i<9;i++){
    		if(501+107*i<e.getX() && e.getX()<501+107*(i+1) 
					&& 575<e.getY() && e.getY()<575+64){
				position=i;
			}
		}
    	
    	this.repaint();
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
    
    
}
